#ifndef PASSWORDMANAGER_H
#define PASSWORDMANAGER_H

#include <QObject>
#include <openssl/rsa.h>
#include <openssl/aes.h>
#include <openssl/md5.h>
#include <openssl/pem.h>
#include <QDebug>
#include <openssl/err.h>
#include <QDir>
#include <QCoreApplication>
#include <algorithm>
#include "utils.h"

#define KEY_LENGTH 2048

#define PUB_KEY_PATH "D://project//c++//browser//key//pubkey.pem"
#define PRI_KEY_PATH "D://project//c++//browser//key//prikey.pem"


#define ENCRYPT_PRI_KEY_ALGORITHM

/**
 * 密码管理器,单例模式
 */
class PasswordManager : public QObject
{
private:
    static PasswordManager instance;
    PasswordManager();
    ~PasswordManager();
    PasswordManager(const PasswordManager&)=delete;
    PasswordManager& operator=(const PasswordManager&)=delete;

public:
    static PasswordManager& getInstance() {
        return instance;
    }

    // 生成rsa密钥对，公钥直接存储到文件中，私钥加密之后存储
    void generateRSAKey(std::string userKey);

    // 加解密
    std::string rsaPubEncrypt(std::string clear_text, RSA *pub_key = nullptr);
    std::string rsaPriDecrypt(std::string cipher_text, RSA * pri_key = nullptr);

    // 读取私钥文件
    RSA * readPrivateKey(std::string userKey);
    // 读取公钥文件
    RSA * readPublicKey();

};

#endif // PASSWORDMANAGER_H
